<?php
declare (strict_types = 1);

namespace app\admin\model;


/**
 * Class SystemMenu
 * @package app\admin\model
 */
class SystemMenu extends BaseModel
{
    public static function checkUserPermission($url){
        return self::field('id,name,url,status')
            ->where('url',$url)
            ->find();
    }
    /**
     * @Descripttion:
     * @param/array $authority
     */
    public static function getMenuByAuthority($authority = null)
    {
        $map = [
            ['status','=',1]
        ];
        if(is_array($authority)){
            $map[] = ['show','=',1];
            $map[] = ['id','in',$authority];
        }
        $menu = self::where($map)
            ->order('type,sort DESC')
            ->select()
            ->toArray();
        $tree = [];
        foreach ($menu as $menu_v){
            //如果是顶级节点直接挂上，否则挂在其父节点下面
            if($menu_v['pid'] == 0){
                $tree[$menu_v['id']] = $menu_v;
            }else{
                $tree[$menu_v['pid']]['children'][] = $menu_v;
            }
        }
        return $tree;
    }
    public static function getParentMenu(){
        return self::where('pid','=',0)
            ->order('sort DESC,id')
            ->select();
    }
    public static function getParentMenuByID($id){
        return self::field('id,name,icon,sort,status')
            ->find($id);
    }
    public static function getChildrenMenuByParentID($pid){
        return self::field('id,pid,name,url,sort,status,show')
            ->where('pid','=',$pid)
            ->order('sort DESC')
            ->select();
    }
    public static function getChildrenMenuByID($id){
        return self::field('id,name,url,sort,status,show')
            ->find($id);
    }
}
